﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace QQ2564874169.Core.Encryption
{
    public static class Decrypt
    {
        public static string Des(string content, string salt)
        {
            if (string.IsNullOrEmpty(salt))
                throw new ArgumentNullException(nameof(salt));
            while (salt.Length < 8)
            {
                salt += salt;
            }

            salt = salt.Substring(0, 8);
            var data = Convert.FromBase64String(content);
            using (var des = DES.Create())
            {
                des.Key = Encoding.ASCII.GetBytes(salt);
                des.IV = Encoding.ASCII.GetBytes(salt);
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
        }

        public static string RSA(string content, string privatekey)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(privatekey);
                var cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
                return Encoding.UTF8.GetString(cipherbytes);
            }
        }

        public static string TripleDES(string content, string key, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(content))
                throw new ArgumentNullException(nameof(content));
            while (key.Length < 24)
            {
                key += key;
            }

            key = key.Substring(0, 24);
            encoding = encoding ?? Encoding.UTF8;
            var data = Convert.FromBase64String(content);
            using (var des = new TripleDESCryptoServiceProvider())
            {
                des.Key = encoding.GetBytes(key);
                des.IV = encoding.GetBytes(key.Substring(0, 8));
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    return encoding.GetString(ms.ToArray());
                }
            }
        }

        public static string Aes(string content, string key, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(content))
                throw new ArgumentNullException(nameof(content));
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException(nameof(key));
            while (key.Length < 32)
            {
                key += key;
            }

            key = key.Substring(0, 32);
            encoding = encoding ?? Encoding.UTF8;

            using (var aesAlg = System.Security.Cryptography.Aes.Create())
            {
                aesAlg.Key = Encoding.ASCII.GetBytes(key);
                aesAlg.IV = Encoding.ASCII.GetBytes(key.Substring(0, 16));

                var encryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                using (var msEncrypt = new MemoryStream(Convert.FromBase64String(content)))
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Read))
                    {
                        using (var swEncrypt = new StreamReader(csEncrypt, encoding))
                        {
                            return swEncrypt.ReadToEnd();
                        }
                    }
                }
            }
        }
    }
}
